package com.android.fastride.manager;

import android.content.Context;
import android.telephony.TelephonyManager;

import com.peng.library.mantis.kit.Kits;

import java.util.UUID;

/**
 * 设备管理器
 * Created by pengyuantao on 2017/9/28 17:04.
 */

public class DeviceManager {

	private static class Holder {
		static final DeviceManager instance = new DeviceManager();
	}

	public static DeviceManager getInstance(){
		return Holder.instance;
	}

	public static final String NAME_DEVICE = "NAME_DEVICE";
	public static final String KEY_DEVICE = "KEY_DEVICE";

	private String deviceId;

	public String getDeviceId (Context context) {
		if (!Kits.Empty.check(deviceId)) {
			return deviceId;
		}
		deviceId = context.getSharedPreferences(NAME_DEVICE, Context.MODE_PRIVATE).getString(KEY_DEVICE, null);
		if (!Kits.Empty.check(deviceId)) {
			return deviceId;
		}
		TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String szImei = TelephonyMgr.getDeviceId();
		if (szImei != null) {
			deviceId = szImei;
		} else {
			deviceId = UUID.randomUUID().toString();
		}
		//保存这个设配id
		context.getSharedPreferences(NAME_DEVICE, Context.MODE_PRIVATE).edit().putString(KEY_DEVICE, deviceId).apply();
		return deviceId;
	}
}
